package com.tianqu.tms.www.car.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarStyle;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.MarketCarGateInfo;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.car.actionform.LicenseplateForm;
import com.tianqu.tms.www.car.viewhelper.CarStockModifyViewhelper;

public class LicenseplateModifyController extends TmsController {
	private Logger logger = Logger.getLogger(LicenseplateModifyController.class);
	public final static int CARCOLORITEMID = 105;
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
			LicenseplateForm form = (LicenseplateForm) uc.getInputForm();
			Car car = Car.getInstance();
			String vin = form.getVin();
			String id = form.getId();
			
			//
			List<MarketCarInfoDto> list = new ArrayList<MarketCarInfoDto>();
			if(!StringUtil.isNullOrBlank(vin)){
				list = car.findCarList(vin, 1);
			}else if(!StringUtil.isNullOrBlank(id)){
				list = car.findCarList(id, 3);
			}
			
			//
			if(list.size() > 1){
				int i = 0;
				if (list.size() > 0 && list != null) {
					for (MarketCarInfoDto dto : list) {
						dto.setNum(++i);
						if(i%2 == 0) dto.setCssFlag(2); 
						else dto.setCssFlag(1);						
					}
				}
				viewhelper.setConfirmFlag(3);
				viewhelper.setMarkCarInfoDtoList(list);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			}else if(list.size() == 1){//找到一条
				viewhelper.setConfirmFlag(2);
				setDetailInfo(list.get(0),viewhelper);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			}else{//没有找到数据
				viewhelper.setConfirmFlag(1);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelper);
			}
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	private void setDetailInfo(MarketCarInfoDto car,CarStockModifyViewhelper viewhelper) throws TmsDaoException{

		//字典信息
		Codes code = Codes.getInstance();
		MarketCarGateInfo marketCarGateInfo = MarketCarGateInfo.getInstance();
		marketCarGateInfo = marketCarGateInfo.findGateInfoBySerial(car.getSerial());
		//会员数据
		Member member = Member.getInstance();
		member = member.findCarScoreByMemberId(String.valueOf(car.getMemberId()));
		//id
		viewhelper.setId(car.getId());		
		viewhelper.setBarCodeNum(car.getBarCodeNum());
		viewhelper.setSerial(car.getSerial());
		//车牌号码
		viewhelper.setLicensePlate(car.getLicensePlate());
		//易碎贴编号
		viewhelper.setTempLicensePlate(getTempLicensePlate(car.getTempLicensePlate()));
		//车辆颜色
		viewhelper.setColor(code.getItemValueByItemIdAndItemCode(CARCOLORITEMID,car.getColor()));
		//车架号
		viewhelper.setVin(car.getVin());
		//车辆名称
		viewhelper.setCarName(car.getCarName());		
		//商户id
		viewhelper.setMemberId(String.valueOf(car.getMemberId()));
		//库存状态
		switch (car.getRegisterStatus()) {
			case 1: viewhelper.setRegisterStatusInfo("已入库");
			break;
			case 2: viewhelper.setRegisterStatusInfo("已出库");
			break;
			case 3: viewhelper.setRegisterStatusInfo("过期");
			break;
			case 4: viewhelper.setRegisterStatusInfo("存档");
			break;
			case 6: viewhelper.setRegisterStatusInfo("登记");
			break;
		}
		// 当前库存量
		viewhelper.setUsedCarPort(member.getUsedCarPort());		
		// 车位总数
		viewhelper.setCarPort(member.getCarPort());
		//商户名称
		viewhelper.setMemberName(member.getMemName());
		String gateType = Registry.getConfigFromDb(TmsConstants.GATE_TYPE);
		viewhelper.setGateType(gateType);
		if((!"1".equals(gateType))&&(!"3".equals(gateType))){
			viewhelper.setGateTypeNo("1");
		}else{
			viewhelper.setGateTypeNo("0");
		}
		//出入库记录
		int i=1;
		List<MarketCarGateInfo> r = new ArrayList<MarketCarGateInfo>();
		for(MarketCarGateInfo info : marketCarGateInfo.findAllGateInfoBySerial(car.getSerial())){
		   if(i>5) break;
		   if(i%2 == 0) info.setCssFlag(2); 
		   else info.setCssFlag(1);
		   info.setNum(i);
		   i++;
		   r.add(info);
		}
		viewhelper.setGateInfoList(r);
		viewhelper.setButtonFlag("true");
	}
	
	private String getTempLicensePlate(String v){
		String r = v;
		if(StringUtil.isNotBlank(v) && v.length() > 8) r = v.substring(v.length() - 8);
		return r;
	}
}
